package com.example.administrator.android2019.cg6service;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import com.example.administrator.android2019.R;

public class ch6_serviceActivity extends AppCompatActivity implements View.OnClickListener {
    Button btn_start,btn_stop;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_ch6_service);
        btn_start=findViewById(R.id.ch6_service_btn1);
        btn_stop=findViewById(R.id.ch6_service_btn2);
        btn_start.setOnClickListener(this);
        btn_stop.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.ch6_service_btn1:
                Intent intent=new Intent(ch6_serviceActivity.this,MyService.class);
                startService(intent);
                /*Toast:参数1 上下文（当前Activity）；
                * 参数2：显示内容
                * 参数3：显示时间（长短）
                * */
                Toast.makeText(this,"服务已开启",Toast.LENGTH_SHORT).show();
                break;
            case R.id.ch6_service_btn2:
                Intent intent2=new Intent(ch6_serviceActivity.this,MyService.class);
                stopService(intent2);
                Toast.makeText(this,"服务已关闭",Toast.LENGTH_SHORT).show();
                break;
             default:
                 break;
        }

    }
}
